package com.ruoyi.web.controller.jy;


import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.http.HttpUtils;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Comparator;

/**
 * 贵重金属实时价格
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/basic/price")
public class BasicPriceController extends BaseController {


    private static final String APP_ID = "wx2296d891ae06fe2a";
    private static final String APP_SECRET = "7b4518dc517b5551cd6059fdf13706e0";
    private String prefix = "jy";

    @GetMapping("/basicPrice")
    public String basicPrice(ModelMap mmap)
    {
        return prefix + "/basicPrice";
    }

    public void weChatOAuth(String  code){
        try {

            String url = String.format(
                    "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code",
                    APP_ID, APP_SECRET, code
            );
            String result= HttpUtils.sendGet(url);
            JSONObject jsonObject = JSONObject.parseObject(result);
            System.out.println(jsonObject.toJSONString());
        }catch (Exception e) {

        }
    }


    private static final String TOKEN = "shorturlrun";

    @GetMapping("wx")
    @ResponseBody
    public String wx(HttpServletRequest request){
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        String code = request.getParameter("code");
        try {
            if (checkSignature(signature, timestamp, nonce)) {
                weChatOAuth(code);
                return echostr;
            } else {
                return "";
            }
        } catch (NoSuchAlgorithmException e) {
            return "";
        }
    }

    private boolean checkSignature(String signature, String timestamp, String nonce) throws NoSuchAlgorithmException {
        String[] tmpArr = {TOKEN, timestamp, nonce};
        Arrays.sort(tmpArr, Comparator.naturalOrder());
        String tmpStr = String.join("", tmpArr);

        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            byte[] sha1Bytes = md.digest(tmpStr.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : sha1Bytes) {
                sb.append(String.format("%02x", b));
            }
            tmpStr = sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return false;
        }

        return tmpStr.equals(signature);
    }

}
